package com.example.services;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    IntentFilter intentFilter;

    MyService serviceBinder;
    Intent i;

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            serviceBinder = ((MyService.MyBinder) iBinder).getService();
            try{
                URL[] urls = new  URL[]{
                        new URL("http://www.ynnu.edu.cn"),
                    new URL("http://www.ynnu.edu.cn"),
                    new URL("http://www.ynnu.edu.cn"),
                    new URL("http://www.ynnu.edu.cn"),
//                    new URL("http://www.ynnu.edu.cn"),
//                    new URL("http://www.ynnu.edu.cn"),
//                    new URL("http://www.ynnu.edu.cn"),
//                    new URL("http://www.ynnu.edu.cn"),
//                    new URL("http://www.ynnu.edu.cn"),
                    new URL("http://www.ynnu.edu.cn"),
                    new URL("http://www.ynnu.edu.cn")
                };

                serviceBinder.urls = urls;
            } catch (MalformedURLException e){
                e.printStackTrace();
            }

            startService(i);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            serviceBinder = null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume() {
        super.onResume();
        intentFilter = new IntentFilter();
        intentFilter.addAction("FILE_DOWNLOADED_ACTION");

        registerReceiver(intentReceiver, intentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(intentReceiver);
    }

    public void onClickStartService(View v){
//        startService(new Intent(getBaseContext(), MyService.class));

//        startService(new Intent(getBaseContext(), MyIntentService.class));

        i = new Intent(getBaseContext(), MyService.class);
        bindService(i,connection, Context.BIND_AUTO_CREATE);
    }

    public void onClickStopService(View v){
//        stopService(new Intent(getBaseContext(), MyService.class));
        unbindService(connection);
    }


    private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(getBaseContext(), "File download!", Toast.LENGTH_LONG).show();
        }
    };
}
